//
//  SNCompilePlanViewController.m
//  ZhuoxinAgriculture
//
//  Created by stone on 16/12/25.
//  Copyright © 2016年 stone. All rights reserved.
//

#import "NSDate+TimeStamp.h"
#import "SNCreateModel.h"
#import "SNUPDateModel.h"
#import "SNPlanListViewController.h"
#import "SNCompilePlanPesticideCell.h"
#import "SNCompilePlanMoreCell.h"
#import "SNCompilePlanFertilizerCell.h"
#import "SNCompilePlanSummaryCell.h"
#import "SNCompilePlanSeedCell.h"

#import "SNCompilePlanSectionHeaderView.h"
#import "SNCompilePlanOtherCell.h"
#import "SNCompilePlanViewController.h"
#import "SNCompilePlanTopView.h"
#import "Masonry.h"
#import "SNCompilePlanCell.h"
static SNProductionPlanListResult *__productionPlanListResultCopy;
@interface SNCompilePlanViewController ()<UITableViewDataSource,UITableViewDelegate,SNCompilePlanPesticideCellDelegate,SNCompilePlanFertilizerCellDelegate>
/** planNo */
@property (nonatomic,copy) NSString * planNo;
/** normalView */
@property (weak, nonatomic) UIView * bottomView;
/** mmButton */
@property(weak, nonatomic) UIButton * saveButton;
/** tableView */
@property (nonatomic,weak) UITableView * tableView;
/** View */
@property (weak, nonatomic)  SNCompilePlanTopView *topView;

/** copy列表模型 */
@property (nonatomic,strong) SNProductionPlanListResult * productionPlanListResultCopy;
/** firstLoad */
@property (nonatomic, assign,getter=isFirstLoad) BOOL firstLoad;
@end

@implementation SNCompilePlanViewController
+ (SNProductionPlanListResult *)getProductionPlanListResultCopy{
    return __productionPlanListResultCopy;
}
+ (void)setProductionPlanListResultCopy:(SNProductionPlanListResult *)productionPlanListResultCopy{
    __productionPlanListResultCopy = productionPlanListResultCopy;
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (self.isFirstLoad) {
        self.firstLoad = NO;
    }else{
        
        self.productionPlanListResultCopy = __productionPlanListResultCopy;
        
        [self.tableView reloadData];
    }
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.firstLoad = YES;
    self.navigationItem.title = @"编辑生产计划";
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = kNormalBackgroundColor;
    //sn_note:=========  ============================ stone 🐳 ===========/
    NSLog(@"%@",self.productionPlanListResult.pestList);
    
    
    //    self.productionPlanListResultCopy = self.productionPlanListResult.copy;
    //    NSLog(@"%@ | productionPlanListResult = %p",self.class,self.productionPlanListResult);
    
    
    __productionPlanListResultCopy = self.productionPlanListResult.copy;
    self.productionPlanListResultCopy = __productionPlanListResultCopy;
    //    NSLog(@"self.productionPlanListResultCopy = %p",self.productionPlanListResultCopy.createTime);
    //    NSLog(@"self.productionPlanListResult = %p",self.productionPlanListResult.createTime);
    
    // Do any additional setup after loading the view, typically from a nib.
    [self addBottomView];
    [self addTopView];
    
    [self.view layoutIfNeeded];
    [self addTableView];
}
- (void)addTableView
{
    /** y坐标 */
    CGFloat y = CGRectGetMaxY(self.topView.frame)+17;
    /** 高度 tableView */
    CGFloat height = self.view.bounds.size.height - y - self.bottomView.frame.size.height;
    /** 样式 tableView */
    UITableViewStyle tableViewStyle = UITableViewStyleGrouped;
    
    CGRect frame = CGRectMake(0, y, self.view.bounds.size.width, height);
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:frame style:tableViewStyle];
    self.tableView = tableView;
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = kNormalBackgroundColor;
    [self setupInit:tableView];
    
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //    tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    //    tableView.separatorColor = [UIColor greenColor];
    [self.view addSubview:tableView];
    
    [self addObserver];
    [self addRequest];
}
- (void)addBottomView
{
    UIView * bottomView = ({
        UIView * view = [[UIView alloc] init];
        
        view.backgroundColor = kNormalBackgroundColor;
        // [self addSubview:view];
        [self.view addSubview:view];
        
        [view mas_makeConstraints:^(MASConstraintMaker * make) {
            make.left.mas_equalTo(self.view.mas_left).offset(0);
            make.right.mas_equalTo(self.view.mas_right).offset(0);
            make.bottom.mas_equalTo(self.view.mas_bottom).offset(0);
            make.height.mas_equalTo(48);
        }];
        
        UIView *splitLineView = [[UIView alloc] init];
        splitLineView.backgroundColor = kSplitLineViewColor;
        [view addSubview:splitLineView];
        [splitLineView mas_makeConstraints:^(MASConstraintMaker * make) {
            make.left.mas_equalTo(view.mas_left).offset(0);
            make.right.mas_equalTo(view.mas_right).offset(0);
            make.top.mas_equalTo(view.mas_top).offset(0);
            make.height.mas_equalTo(1);
        }];
        
        view;
    });
    self.bottomView = bottomView;
    
    
    
    UIButton * saveButton = ({
        UIButton * button = [[UIButton alloc] init];
        button.layer.cornerRadius = 4;
        button.layer.masksToBounds = YES;
        [button setTitle:@"全部保存" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        button.titleLabel.font = [UIFont systemFontOfSize:15];
        
        [button setBackgroundImage:[SNUtils imageWithString:@"67B245"] forState:UIControlStateNormal];
        
        [button addTarget:self action:@selector(saveButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        
        [bottomView addSubview:button];
        
        [button mas_makeConstraints:^(MASConstraintMaker * make) {
            make.left.mas_equalTo(bottomView.mas_left).offset(30);
            make.right.mas_equalTo(bottomView.mas_right).offset(-30);
            make.top.mas_equalTo(bottomView.mas_top).offset(8);
            make.bottom.mas_equalTo(bottomView.mas_bottom).offset(-8);
        }];
        
        button;
    });
    
    self.saveButton = saveButton;
}
- (void)addCompletionAlertController
{
    NSString * title = @"";
    NSString * message = @"该生产计划已提交, 请耐心等待审批结果!";
    NSString * actionButtonTitle = @"确定";
    
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    
    UIAlertAction * action = [UIAlertAction actionWithTitle:actionButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        // 确定之后需要 处理的code
        [self.navigationController popViewControllerAnimated:YES];
    }];
    
    [alertController addAction:action];
    
    [self presentViewController:alertController animated:YES completion:nil];
 
}
- (void)addCommitAlertController
{
    NSString * title = @"";
    NSString * message = @"是否确定提交该生产计划 ?";
    NSString * actionButtonTitle1 = @"取消";
    NSString * actionButtonTitle2 = @"确定";
    
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction * action1 = [UIAlertAction actionWithTitle:actionButtonTitle1 style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        // 取消之后需要 处理的code
        
    }];
    UIAlertAction * action2 = [UIAlertAction actionWithTitle:actionButtonTitle2 style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        // 确定之后需要 处理的code
        
        if (self.isFromNew) {
            
            [[ZXHTTPManager sharedManager] plancommitWithPlanNo:self.planNo operatorName:kUserInfo.userId completed:^(NSDictionary *jsonDict, NSError *error) {
                
                logError;
                
                if ([jsonDict[@"status"] integerValue] == 0) {
                    // !self.reloadDataOfTableView ?: self.reloadDataOfTableView();
                    [self addCompletionAlertController];
                    
                }
                
            }];
        }else{
            [[ZXHTTPManager sharedManager] plancommitWithPlanNo:self.productionPlanListResult.planNo operatorName:kUserInfo.userId completed:^(NSDictionary *jsonDict, NSError *error) {
                
                logError;
                
                if ([jsonDict[@"status"] integerValue] == 0) {
                    // !self.reloadDataOfTableView ?: self.reloadDataOfTableView();
                    [self addCompletionAlertController];
                    
                }
                
            }];
        }
    }];
    
    [alertController addAction:action1];
    [alertController addAction:action2];
    
    [self presentViewController:alertController animated:YES completion:nil];
    
}
-(void)saveButtonClicked:(UIButton *)sender {
    NSLog(@"%s", __func__);
    
    if ([sender.currentTitle isEqualToString:@"提交审批"]) {
        //TODO: 提交接口
        [self addCommitAlertController];
        return;
    }
    
    
    //TODO: 保存, 模型赋值
    if (self.isFromNew) {
        SNProductionPlanListModel * productionPlanListModel =  [SNPlanListViewController getProductionPlanListModel];
        SNProductionPlanListResult * resultCopy = self.productionPlanListResultCopy;
        
        NSMutableArray<SNProductionPlanListResult *> * arrM = productionPlanListModel.result.mutableCopy;
        [arrM addObject:resultCopy];
        productionPlanListModel.result = arrM;
        [SNPlanListViewController setProductionPlanListModel:productionPlanListModel];
        
        //-------------
        NSMutableDictionary * dict = resultCopy.mj_keyValues;
        
        SNCreateModel * model = [SNCreateModel mj_objectWithKeyValues:dict];
        model.operator = kUserInfo.userId;
        model.createTime = [NSDate new].dateToString(@"yyyy-MM-dd HH:mm:ss");
        model.custNo = self.plantingDecisionMakerResult.custNo;
        model.seedPerUse = @"66";
        [model.fertList enumerateObjectsUsingBlock:^(SNCreateFertlist * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
           obj.fertPerUse = @"66";
        }];
        [model.pestList enumerateObjectsUsingBlock:^(SNCreatePestlist * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            obj.pestPerUse = @"66";
        }];
        model.status = @"0";
        
        dict = model.mj_keyValues;
        //MARK: - 注意 创建接口和 更新接口不一样...
        [[ZXHTTPManager sharedManager] createplanWithDict:dict completed:^(NSDictionary *jsonDict, NSError *error) {
            
            logError;
            
            NSLog(@"%@",jsonDict);
            if ([jsonDict[@"status"] integerValue] == 0) {
                NSLog(@"jsonDict[@\"result\"] = %@",jsonDict[@"result"]);
                NSArray<NSDictionary *> * array = jsonDict[@"result"];
                self.planNo = array.lastObject[@"planNo"];
                NSLog(@"self.planNo = %@",self.planNo);
                // 成功
                [self.saveButton setTitle:@"提交审批" forState:UIControlStateNormal];
                
                
                [self.tableView reloadData];
            }else{
                [self addErrorAlertViewController];
            }
            
        }];
        
        // [self.navigationController popViewControllerAnimated:YES];
    }else{
        [self normalOperation];
    }
}
- (void)normalOperation
{
    NSLog(@"%@ | productionPlanListResult = %p",self.class,self.productionPlanListResult);
    NSLog(@"%@",self.productionPlanListResult.pestList);
    NSLog(@"%@",self.productionPlanListResultCopy.pestList);
    
    SNProductionPlanListModel * productionPlanListModel =  [SNPlanListViewController getProductionPlanListModel];
    SNProductionPlanListResult * resultCopy = self.productionPlanListResultCopy;
    SNProductionPlanListResult * resultOriginal = productionPlanListModel.result[self.indexPath.section];
    
    //---
    resultOriginal.fertList = resultCopy.fertList;
    resultOriginal.landList = resultCopy.landList;
    resultOriginal.pestList = resultCopy.pestList;
    resultOriginal.createTime = resultCopy.createTime;
    resultOriginal.cropType = resultCopy.cropType;
    resultOriginal.custNo = resultCopy.custNo;
    resultOriginal.fullName = resultCopy.fullName;
    resultOriginal.operatorName = resultCopy.operatorName;
    resultOriginal.planName = resultCopy.planName;
    resultOriginal.planNo = resultCopy.planNo;
    resultOriginal.plantingArea = resultCopy.plantingArea;
    resultOriginal.seedBrand = resultCopy.seedBrand;
    resultOriginal.seedManu = resultCopy.seedManu;
    resultOriginal.seedPerUse = resultCopy.seedPerUse;
    resultOriginal.seedPrice = resultCopy.seedPrice;
    resultOriginal.seedTotal = resultCopy.seedTotal;
    resultOriginal.startTime = resultCopy.startTime;
    resultOriginal.status = resultCopy.status;
    resultOriginal.year = resultCopy.year;
    resultOriginal.fertTotal = resultCopy.fertTotal;
    resultOriginal.plantTypes = resultCopy.plantTypes;
    resultOriginal.cropTypeName = resultCopy.cropTypeName;
    //---
    
    [SNPlanListViewController setProductionPlanListModel:productionPlanListModel];
    
    NSMutableDictionary * dict = resultCopy.mj_keyValues;
    
    SNUPDateModel * model = [SNUPDateModel mj_objectWithKeyValues:dict];
    // model.landList = nil;
    model.operator = kUserInfo.userId;
    model.landList = model.landList.count == 0 ? nil:model.landList;
    dict = model.mj_keyValues;
    
    [[ZXHTTPManager sharedManager] updateplanWithDict:dict completed:^(NSDictionary *jsonDict, NSError *error) {
        
        logError;
        
        NSLog(@"%@",jsonDict);
        if ([jsonDict[@"status"] integerValue] == 0) {
            // 成功
            [self.saveButton setTitle:@"提交审批" forState:UIControlStateNormal];
            
            
            [self.tableView reloadData];
        }else{
            NSLog(@"%@",jsonDict[@"message"]);
        }
        
    }];
    
//    [self.navigationController popViewControllerAnimated:YES];
}

- (void)addErrorAlertViewController
{
    NSString * title = @"";
    NSString * message = @"生产计划信息填写不完整, 请补全信息后提交!";
    NSString * actionButtonTitle = @"确定";
    
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction * action = [UIAlertAction actionWithTitle:actionButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        // 确定之后需要 处理的code
        
    }];
    
    [alertController addAction:action];
    
    [self presentViewController:alertController animated:YES completion:nil];
}

- (void)addTopView
{
    SNCompilePlanTopView *topView = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([SNCompilePlanTopView class]) owner:nil options:nil] lastObject];
    self.topView = topView;
    topView.plantingDecisionMakerResult = self.plantingDecisionMakerResult;
    [self.view addSubview:topView];
    
    [topView mas_makeConstraints:^(MASConstraintMaker * make) {
        make.left.mas_equalTo(self.view.mas_left).offset(12);
        make.right.mas_equalTo(self.view.mas_right).offset(-12);
        make.top.mas_equalTo(self.view.mas_top).offset(64);
    }];
}

//sn_note:========= tableView ============================ stone 🐳 ===========/
#pragma mark - <addRequest>
- (void)addRequest {
    // addRequest
}
#pragma mark - <addObserver>
- (void)addObserver {
    // addObserver
}
- (void)dealloc {
    NSLog(@"%@ %s %@",[self class], __func__ , @"im dying");
}
/** 初始化 tableView */
- (void)setupInit:(UITableView *)tableView
{
    //MARK: - 设置tableHeaderView
    //    {
    //        UIView * tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 17)];
    //        tableHeaderView.backgroundColor = kNormalBackgroundColor;
    //        tableView.tableHeaderView = tableHeaderView;
    //    }
    
    //MARK: - 注册
    {
        /** xib 注册cell */
        registerForCellFromeNib(tableView,SNCompilePlanCell);
        registerForCellFromeNib(tableView,SNCompilePlanOtherCell);
        registerForCellFromeNib(tableView,SNCompilePlanSeedCell);
        registerForCellFromeNib(tableView,SNCompilePlanSummaryCell);
        registerForCellFromeNib(tableView,SNCompilePlanFertilizerCell);
        registerForCellFromeNib(tableView,SNCompilePlanMoreCell);
        registerForCellFromeNib(tableView,SNCompilePlanPesticideCell);
        
        /** class 注册cell */
        //  registerForCellFromeClass(tableView,UITableViewCell);
        
        /** xib 注册 headerSection */
        registerForHeaderFooterViewFromeNib(tableView,SNCompilePlanSectionHeaderView);
        
        
        /** class 注册 headerSection */
        //    registerForHeaderFooterViewFromeClass(tableView,UITableViewHeaderFooterView);
        
        /** xib 注册 footerSection */
        //    registerForHeaderFooterViewFromeNib(tableView,UITableViewHeaderFooterView);
        /** class 注册 footerSection */
        //    registerForHeaderFooterViewFromeClass(tableView,UITableViewHeaderFooterView);
    }
    //MARK: - 设置高度
    {
        /** header */
        //        tableView.sectionHeaderHeight = 17;
        tableView.estimatedSectionHeaderHeight = 100;
        tableView.sectionHeaderHeight = UITableViewAutomaticDimension;
        /** cell */
        tableView.estimatedRowHeight = 100;
        tableView.rowHeight = UITableViewAutomaticDimension;
        /** footer */
        tableView.sectionFooterHeight = 17;
        // tableView.estimatedSectionFooterHeight = 100;
        // tableView.sectionFooterHeight = UITableViewAutomaticDimension;
    }
}

#pragma mark - <UITableViewDataSource>
/** 组数 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 4;
}
/** 行数 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (section) {
        case 0:
            if (self.productionPlanListResultCopy.cropTypeName.length > 0 ) {
                return 1;
            }else{
                return 0;
            }
        case 1:
            if (self.productionPlanListResultCopy.seedBrand.length > 0 ) {
                return 1;
            }else{
                return 0;
            }
        case 2:
            if ([self.saveButton.currentTitle isEqualToString:@"提交审批"]) {
                return self.productionPlanListResultCopy.fertList.count;
            }else{
                
                return self.productionPlanListResultCopy.fertList.count+1;
            }
            break;
        case 3:
            if ([self.saveButton.currentTitle isEqualToString:@"提交审批"]) {
                return self.productionPlanListResultCopy.pestList.count;
            }else{
                
                return self.productionPlanListResultCopy.pestList.count+1;
            }
            
            break;
        default:
            break;
    }
    
    return self.rowCount;
}
//MARK: -
//MARK: - cell
/** cell */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell * cell = nil;
    switch (indexPath.section) {
        case 0:
        {
            SNCompilePlanSummaryCell * cellTemp = DequeueForCell(SNCompilePlanSummaryCell);
            [cellTemp configCellWithTitle:@"概要信息" imageName:@"05-earth"];
            cellTemp.nav = self.navigationController;
            cellTemp.productionPlanListResult = self.productionPlanListResultCopy;
            cell = cellTemp;
        }
            break;
        case 1:
        {
            SNCompilePlanSeedCell * cellTemp = (SNCompilePlanSeedCell *)DequeueForCell(SNCompilePlanSeedCell);
            [cellTemp configCellWithTitle:@"种子信息" imageName:@"05-seed"];
            cellTemp.nav = self.navigationController;
            cellTemp.productionPlanListResult = self.productionPlanListResultCopy;
            cell = cellTemp;
        }
            break;
        case 2:
        {
            
            if ([self.saveButton.currentTitle isEqualToString:@"提交审批"]) {
                SNCompilePlanFertilizerCell * cellTemp = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([SNCompilePlanFertilizerCell class])];
                cellTemp.delegate = self;
                NSString *title = [NSString stringWithFormat:@"肥料信息( %zd )",indexPath.row+1];
                [cellTemp configCellWithTitle:title imageName:@"11-flow" indexPath:indexPath saveButtonCurrentTitle:self.saveButton.currentTitle];
                
                cellTemp.productionPlanListResult = self.productionPlanListResultCopy;
                cellTemp.nav = self.navigationController;
                
                cell = cellTemp;
            }else{
                if (indexPath.row == self.productionPlanListResultCopy.fertList.count) {
                    SNCompilePlanMoreCell * cellTemp = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([SNCompilePlanMoreCell class])];
                    cellTemp.nav = self.navigationController;
                    cellTemp.indexPath = indexPath;
                    cellTemp.productionPlanListResult = self.productionPlanListResultCopy;
                    cell = cellTemp;
                }else{
                    SNCompilePlanFertilizerCell * cellTemp = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([SNCompilePlanFertilizerCell class])];
                    cellTemp.delegate = self;
                    NSString *title = [NSString stringWithFormat:@"肥料信息( %zd )",indexPath.row+1];
                    [cellTemp configCellWithTitle:title imageName:@"11-flow" indexPath:indexPath saveButtonCurrentTitle:self.saveButton.currentTitle];
                    
                    cellTemp.productionPlanListResult = self.productionPlanListResultCopy;
                    cellTemp.nav = self.navigationController;
                    
                    cell = cellTemp;
                }
            }
        }
            break;
        case 3:
        {
            
            if ([self.saveButton.currentTitle isEqualToString:@"提交审批"]) {
                SNCompilePlanPesticideCell * cellTemp = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([SNCompilePlanPesticideCell class])];
                cellTemp.delegate = self;
                NSString *title = [NSString stringWithFormat:@"农药信息( %zd )",indexPath.row+1];
                [cellTemp configCellWithTitle:title imageName:@"icon_pesticide" indexPath:indexPath saveButtonCurrentTitle:self.saveButton.currentTitle];
                cellTemp.productionPlanListResult = self.productionPlanListResultCopy;
                
                cellTemp.nav = self.navigationController;
                cell = cellTemp;
            }else{
                if (indexPath.row == self.productionPlanListResultCopy.pestList.count) {
                    SNCompilePlanMoreCell * cellTemp = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([SNCompilePlanMoreCell class])];
                    cellTemp.nav = self.navigationController;
                    cellTemp.indexPath = indexPath;
                    cellTemp.productionPlanListResult = self.productionPlanListResultCopy;
                    cell = cellTemp;
                }else{
                    SNCompilePlanPesticideCell * cellTemp = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([SNCompilePlanPesticideCell class])];
                    cellTemp.delegate = self;
                    NSString *title = [NSString stringWithFormat:@"农药信息( %zd )",indexPath.row+1];
                    [cellTemp configCellWithTitle:title imageName:@"icon_pesticide" indexPath:indexPath saveButtonCurrentTitle:self.saveButton.currentTitle];
                    cellTemp.productionPlanListResult = self.productionPlanListResultCopy;
                    
                    cellTemp.nav = self.navigationController;
                    cell = cellTemp;
                }
            }
            
            
        }
            break;
            
        default:
            break;
    }
    
    
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}
- (void)addDeleteAlertView:(NSString *)messageString confirmBlock:(void(^)())confirmBlock
{
    NSString * title = @"";
    NSString * message = [NSString stringWithFormat:@"是否确定删除该条%@信息",messageString];
    NSString * actionButtonTitle1 = @"取消";
    NSString * actionButtonTitle2 = @"确定";
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction * action1 = [UIAlertAction actionWithTitle:actionButtonTitle1 style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        // 取消之后需要 处理的code
        
    }];
    UIAlertAction * action2 = [UIAlertAction actionWithTitle:actionButtonTitle2 style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        // 确定之后需要 处理的code
        !confirmBlock ?: confirmBlock();
        
    }];
    
    [alertController addAction:action1];
    [alertController addAction:action2];
    [self presentViewController:alertController animated:YES completion:nil];
}
#pragma mark - <SNCompilePlanPesticideCellDelegate>
- (void)deleteInfomationWithIndexPath:(NSIndexPath *)indexPath object:(NSObject *)object{
    
    if ([object isKindOfClass:[SNCompilePlanFertilizerCell class]]) {
        @weakify_self
        [self addDeleteAlertView:@"肥料" confirmBlock:^{
            @strongify_self
            NSMutableArray<SNProductionPlanListFertlist *> * arrM  = self.productionPlanListResultCopy.fertList.mutableCopy;
            [arrM removeObjectAtIndex:indexPath.row];
            self.productionPlanListResultCopy.fertList = arrM;
            
            [self.tableView reloadData];
        }];
        
    }else if ([object isKindOfClass:[SNCompilePlanPesticideCell class]]) {
        
        @weakify_self
        [self addDeleteAlertView:@"农药" confirmBlock:^{
            @strongify_self
            NSMutableArray<SNProductionPlanListPestlist *> * arrM  = self.productionPlanListResultCopy.pestList.mutableCopy;
            [arrM removeObjectAtIndex:indexPath.row];
            self.productionPlanListResultCopy.pestList = arrM;
            
            [self.tableView reloadData];
        }];
        
    }
}
#pragma mark - <UITableViewDelegate>
/** 自定义section header */
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UITableViewHeaderFooterView * headerView = nil;
    
    switch (section) {
        case 0:
        {
            SNCompilePlanSectionHeaderView * headerViewTemp = DequeueForHeaderFooterView(SNCompilePlanSectionHeaderView);
            [headerViewTemp configCellWithTitle:@"概要信息" imageName:@"05-earth" productionPlanListResult:self.productionPlanListResultCopy saveButtonCurrentTitle:self.saveButton.currentTitle];
            headerViewTemp.nav = self.navigationController;
            headerView = headerViewTemp;
        }
            break;
        case 1:
        {
            SNCompilePlanSectionHeaderView * headerViewTemp = DequeueForHeaderFooterView(SNCompilePlanSectionHeaderView);
            [headerViewTemp configCellWithTitle:@"种子信息" imageName:@"05-seed" productionPlanListResult:self.productionPlanListResultCopy saveButtonCurrentTitle:self.saveButton.currentTitle];
            headerViewTemp.nav = self.navigationController;
            headerView = headerViewTemp;        }
            break;
        case 2:
        {
            SNCompilePlanSectionHeaderView * headerViewTemp = DequeueForHeaderFooterView(SNCompilePlanSectionHeaderView);
            [headerViewTemp configCellWithTitle:@"肥料信息" imageName:@"05-fertilizer" productionPlanListResult:self.productionPlanListResultCopy saveButtonCurrentTitle:self.saveButton.currentTitle];
            headerViewTemp.nav = self.navigationController;
            headerView = headerViewTemp;
        }
            break;
        case 3:
        {
            SNCompilePlanSectionHeaderView * headerViewTemp = DequeueForHeaderFooterView(SNCompilePlanSectionHeaderView);
            [headerViewTemp configCellWithTitle:@"农药信息" imageName:@"05-medicine" productionPlanListResult:self.productionPlanListResultCopy saveButtonCurrentTitle:self.saveButton.currentTitle];
            headerViewTemp.nav = self.navigationController;
            
            headerView = headerViewTemp;
        }
            break;
            
        default:
            break;
    }
    
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return -1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 17;
}
/** 选中一行 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%s", __func__);
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
/** 取消 选中 */
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0) {
    NSLog(@"%s", __func__);
}
/** 可编辑 */
//- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return YES;
//}
/** 删除样式 */
//- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return UITableViewCellEditingStyleDelete;
//}
/** 删除当前行 */
//- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
//
//    if (editingStyle == UITableViewCellEditingStyleDelete) {
//
//        [self.models removeObjectAtIndex:indexPath.row];
//
//        [tableView deleteRowsAtIndexPaths:@[ indexPath ] withRowAnimation:UITableViewRowAnimationLeft];
//}

@end
